import java.util.Random;

public class MineFieldGenerator {
    private int size;        // 游戏区域大小
    private int mineCount;   // 地雷数量
    private Random random;   // 随机数生成器

    public MineFieldGenerator(int size, int mineCount) {
        this.size = size;
        this.mineCount = mineCount;
        this.random = new Random();
    }

    // 生成地雷场
    public int[][] generateMineField() {
        int[][] mineField = new int[size][size];

        // 初始化地雷场，全部为0
        initializeMineField(mineField);

        // 随机放置地雷
        placeMines(mineField);

        // 计算每个格子周围的地雷数量
        calculateNumbers(mineField);

        return mineField;
    }

    // 初始化地雷场，全部设为0
    private void initializeMineField(int[][] mineField) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                mineField[i][j] = 0;
            }
        }
    }

    // 随机放置地雷
    private void placeMines(int[][] mineField) {
        int minesPlaced = 0;

        while (minesPlaced < mineCount) {
            int x = random.nextInt(size);
            int y = random.nextInt(size);

            // 如果该位置没有地雷，则放置地雷
            if (mineField[x][y] != -1) {
                mineField[x][y] = -1;  // -1 表示地雷
                minesPlaced++;
            }
        }
    }

    // 计算每个格子周围的地雷数量
    private void calculateNumbers(int[][] mineField) {
        // 定义8个方向：上、下、左、右、左上、右上、左下、右下
        int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
        int[] dy = {-1, 0, 1, -1, 1, -1, 0, 1};

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                // 如果当前格子是地雷，跳过
                if (mineField[i][j] == -1) {
                    continue;
                }

                int count = 0;

                // 检查周围8个方向
                for (int k = 0; k < 8; k++) {
                    int ni = i + dx[k];
                    int nj = j + dy[k];

                    // 检查坐标是否在有效范围内
                    if (ni >= 0 && ni < size && nj >= 0 && nj < size) {
                        if (mineField[ni][nj] == -1) {
                            count++;
                        }
                    }
                }

                mineField[i][j] = count;
            }
        }
    }

    // 打印地雷场（用于调试）
    public void printMineField(int[][] mineField) {
        System.out.println("实际地雷场布局：");
        System.out.print("  ");
        for (int i = 0; i < size; i++) {
            System.out.print(i + " ");
        }
        System.out.println();

        for (int i = 0; i < size; i++) {
            System.out.print(i + " ");
            for (int j = 0; j < size; j++) {
                if (mineField[i][j] == -1) {
                    System.out.print("X ");
                } else {
                    System.out.print(mineField[i][j] + " ");
                }
            }
            System.out.println();
        }
    }

    // 获取地雷数量
    public int getMineCount() {
        return mineCount;
    }

    // 获取游戏区域大小
    public int getSize() {
        return size;
    }
}